


#include "CxPackageData.h"
#include <memory.h>
#include <zlib.h>






using namespace ImitateTansfer;


CxPackage *CxPackage::Package(char *src,size_t	size,int _row,int _col,int type,int	mode)
{
	return	new CxPackageData(src,size,_row,_col,type,mode);
}


CxPackageData::~CxPackageData()
{
	free(m_pdata);
}

CxPackageData::CxPackageData(char *src,size_t size,int _row,int _col,int type,int copymode):
	m_size(size),
	m_isCompress(false),m_isPackage(false)
{
	CX_ASSERT(src);
	CX_ASSERT(size);

	char *data=src;

	if(copymode)
	{
		data=(char *)malloc(size+26);

		memcpy(data,src,size);
	}

	m_pdata=data;

	Compress();

	PackageData(_row,_col,type);

	CX_ASSERT(m_isCompress);
	CX_ASSERT(m_isPackage);

}


bool	CxPackageData::Compress()
{
	
	CX_ASSERT(m_pdata);

	unsigned char *dest=NULL;

	dest=(unsigned char*)malloc(m_size+26);

	unsigned long rtsize=m_size+10;	

	CX_ASSERT(dest);

	int	result=compress(dest+16,&rtsize,(unsigned char *)m_pdata,m_size);

	CX_ASSERT(!result);

	m_pdata=(char *)dest;

	m_size=rtsize;

	m_isCompress=true;

	return true;
}


bool	CxPackageData::PackageData(int _row,int  _col,int  type)
{
	m_pdata[0]=0x5f;
	m_pdata[1]=0x6f;

	(*(int *)&m_pdata[2])=_row;
	(*(int *)&m_pdata[6])=_col;
	(*(short *)&m_pdata[10])=type;

	*((unsigned long *)&m_pdata[12])=m_size;

	m_size+=16;
	m_isPackage=true;

	return true;
}
